home *** CD-ROM | disk | FTP | other *** search
/ Amiga Tools 4 / Amiga Tools 4.iso / tools / netzwerk / tcp-ip / ipdial_v1.9 / deviceio.c < prev    next >
C/C++ Source or Header  |  1996-02-26  |  5KB  |  205 lines

  1. /**
  2. ***  IPDial     Script program for initializing a SLIP connection
  3. ***  Copyright  (C)   1994    Jochen Wiedmann
  4. ***
  5. ***  This program is free software; you can redistribute it and/or modify
  6. ***  it under the terms of the GNU General Public License as published by
  7. ***  the Free Software Foundation; either version 2 of the License, or
  8. ***  (at your option) any later version.
  9. ***
  10. ***  This program is distributed in the hope that it will be useful,
  11. ***  but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ***  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13. ***  GNU General Public License for more details.
  14. ***
  15. ***  You should have received a copy of the GNU General Public License
  16. ***  along with this program; if not, write to the Free Software
  17. ***  Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  18. ***
  19. ***
  20. ***
  21. ***  This file implements something like a device class. It's only
  22. ***  reason is the lack of a possibility to determine the real state
  23. ***  of an IORequest. (Sent? Inactive? Whatever else?)
  24. ***
  25. ***
  26. ***  Computer: Amiga 1200                       Compiler: Dice 3.01
  27. ***
  28. ***  Author:    Jochen Wiedmann
  29. ***             Am Eisteich 9
  30. ***             72555 Metzingen
  31. ***             Germany
  32. ***
  33. ***             Phone: (+0049) 7123 / 14881
  34. ***             Internet: wiedmann@neckar-alb.de
  35. **/
  36.  
  37. /**
  38. ***  Include files
  39. **/
  40. #ifndef IPDIAL_H
  41. #include "IPDial.h"
  42. #endif
  43. #include <exec/devices.h>
  44. #include <exec/io.h>
  45. /**
  46. ***  We use the structure below for device IO, because I don't see
  47. ***  any way to check an IORequest whether it must be aborted, before
  48. ***  using or deleting it.
  49. **/
  50. struct MyIORequest
  51.   ULONG Used;
  52.   ULONG Signal;
  53.   ULONG DeviceOpen;
  54.   struct IORequest *Req;
  55. };
  56. /**
  57. ***  This is the WaitIO() equivalent.
  58. **/
  59. BYTE DeviceIOWait(APTR wreq)
  60.   struct MyIORequest *req = wreq;
  61.   BYTE result;
  62.  
  63.   result = WaitIO(req->Req);
  64.   req->Used = FALSE;
  65.   return(result);
  66. }
  67. /**
  68. ***  This is the AbortIO() equivalent.
  69. **/
  70. VOID DeviceIOAbort(APTR areq)
  71. { struct MyIORequest *req = areq;
  72.  
  73.   if (req  &&  req->Used  &&  req->Req)
  74.   { 
  75.     AbortIO(req->Req);
  76.     DeviceIOWait(req);
  77.   }
  78.   /**
  79.   ***  The following is needed for reasons I don't understand
  80.   ***  really.
  81.   **/
  82.   SetSignal(0, req->Signal);
  83. }
  84. /**
  85. ***  This is the SendIO() equivalent.
  86. **/
  87. VOID DeviceIOSend(APTR sreq, UWORD Command)
  88.   struct MyIORequest *req = sreq;
  89.  
  90.   if (req->Used)
  91.   { 
  92.     fprintf(stderr, "Internal error: IORequest in use.\n");
  93.     exit(20);
  94.   }
  95.   req->Used = TRUE;
  96.   req->Req->io_Command = Command;
  97.   SendIO(req->Req);
  98. }
  99. /**
  100. ***  This is the DoIO() equivalent.
  101. **/
  102. BYTE DeviceIODo(APTR dreq, UWORD Command)
  103. {
  104.   BYTE   status; 
  105.   struct MyIORequest *req = dreq;
  106.  
  107.   if (req->Used)
  108.   { 
  109.     fprintf(stderr, "Internal error: IORequest in use.\n");
  110.     exit(20);
  111.   }
  112.   req->Req->io_Command = Command;
  113.   status = DoIO(req->Req);
  114.   Delay(10);
  115.   return(status);
  116. }
  117. /**
  118. ***  This is the DeleteIORequest() equivalent.
  119. **/
  120. VOID DeviceIODelete(APTR dreq)
  121.   struct MyIORequest *req = dreq;
  122.  
  123.   if (req)
  124.   { 
  125.     DeviceIOAbort(req);
  126.     if (req->DeviceOpen)
  127.     { 
  128.       CloseDevice(req->Req);
  129.     }
  130.     if (req->Req)
  131.     { 
  132.       struct MsgPort *port = req->Req->io_Message.mn_ReplyPort;
  133.  
  134.       DeleteIORequest(req->Req);
  135.       DeleteMsgPort(port);
  136.     }
  137.     free(req);
  138.   }
  139. }
  140. /**
  141. ***  This is the CreateIORequest() equivalent.
  142. **/
  143. APTR DeviceIOCreate(ULONG Size)
  144.   struct MyIORequest *req;
  145.  
  146.   if ((req = malloc(sizeof(*req))))
  147.   { 
  148.     struct MsgPort *port;
  149.  
  150.     req->Used = FALSE;
  151.     req->DeviceOpen = FALSE;
  152.     if ((port = CreateMsgPort()))
  153.     { 
  154.       req->Signal = (1 << port->mp_SigBit);
  155.       if ((req->Req = CreateIORequest(port, Size)))
  156.       { 
  157.         return(req);
  158.       }
  159.       DeleteMsgPort(port);
  160.     }
  161.     free(req);
  162.   }
  163.   return(NULL);
  164. }
  165. /**
  166. ***  This is the OpenDevice() equivalent.
  167. **/
  168. BYTE DeviceIOOpen(STRPTR devname, ULONG unit, APTR oreq, ULONG flags)
  169.   struct MyIORequest *req = oreq;
  170.   BYTE error;
  171.  
  172.   if (req->DeviceOpen)
  173.   { 
  174.     fprintf(stderr, "Internal error: Device already open.\n");
  175.     exit(20);
  176.   }
  177.   if (!(error = OpenDevice(devname, unit, req->Req, flags)))
  178.   { 
  179.     req->DeviceOpen = TRUE;
  180.   }
  181.   return(error);
  182. }
  183. /**
  184. ***  This function returns the IORequest attached to a struct
  185. ***  MyIORequest.
  186. **/
  187. struct IORequest *DeviceIOReq(APTR rreq)
  188.   return(((struct MyIORequest *)rreq)->Req);
  189. }
  190. /**
  191. ***  This function returns an IORequest's signal. (Not the
  192. ***  signal number.)
  193. **/
  194. ULONG DeviceIOSignal(APTR sreq)
  195.   return(((struct MyIORequest *) sreq)->Signal);
  196. }
  197.